package com.example.houseman.controller;


import com.example.houseman.entity.TSfContract;
import com.example.houseman.enums.AccountTypeEnum;
import com.example.houseman.enums.CertificateType;
import com.example.houseman.enums.OwnershipTypeEnum;
import com.example.houseman.enums.TenantManage;
import com.example.houseman.service.ITSfContractService;
import com.example.houseman.service.ITSfFinalStatementDetailService;
import com.example.houseman.service.ITSfTerminationService;
import com.github.pagehelper.PageInfo;
import com.org.dto.*;
import com.org.util.PayMentPlanGenerator;
import com.org.vo.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龚梦
 * @since 2021-04-02
 */
@RestController
@RequestMapping("/hou/TSfContractController")
public class TSfContractController {
    @Autowired
    ITSfContractService itSfContractService;

    @Autowired
    ITSfTerminationService itSfTerminationService;

    @Autowired
    ITSfFinalStatementDetailService itSfFinalStatementDetailService;

    //收房列表查询
    @RequestMapping("list")
    public ResultEntity list(TsfContractDto tsfContractDto){

        PageInfo<TSfContract> list = itSfContractService.getlist(tsfContractDto);
        System.out.print(list);
        return ResultEntity.ok("200","成功",list);

    }

    //业主列表查询
    @RequestMapping("yezhulist")
    public ResultEntity yezhulist(TsfContractDto tsfContractDto){

        PageInfo<TSfContract> list = itSfContractService.getlists(tsfContractDto);

        return ResultEntity.ok("200","成功",list);

    }

    //解约列表查询
    @RequestMapping("jie")
    public ResultEntity jie(TsfContractDto tsfContractDto){
        PageInfo<TSfContract> ss = itSfContractService.jie(tsfContractDto);
        return ResultEntity.ok("200","成功",ss);
    }

    //解约中的收房合同回显
    @RequestMapping("getOne")
    public TSfContract getOne(Long id){
        System.out.println(id+"======");
        TSfContract tSfContract = itSfContractService.getById(id);
        return tSfContract;
    }

    @RequestMapping("add")
    public boolean add(@RequestBody TSfContract tSfContract){
        try{
            itSfContractService.save(tSfContract);
            return true;
        }catch (Exception e){
            return false;
        }
    }


    @RequestMapping("selectlist")
    public ResultEntity contractlist(@RequestBody TsfContractDto contractDto){
        PageInfo<TSfContract>page=itSfContractService.selectList(contractDto);
        return ResultEntity.ok("200","查询",page);
    }
    @RequestMapping("updateStatus")
    public ResultEntity upadateStatus(@RequestBody TsfContractDto contractDto){
        itSfContractService.updateStatus(contractDto);
        return ResultEntity.ok("修改成功");
    }

    /**
     * 获取所有权类型
     * @return
     */
    @RequestMapping("/houseAllTypeEnum")
    public ResultEntity houseAllTypeEnum() {
        return ResultEntity.ok("200","查询所有权类型成功", OwnershipTypeEnum.values());
    }


    /**
     * 获取所有的身份证类型
     * @return
     */
    @RequestMapping("/certificateType")
    public ResultEntity certificateType() {
        return ResultEntity.ok("200","查询身份证类型成功", CertificateType.values());
    }

    /**
     * 获取所有租房类型类型
     * @return
     */
    @RequestMapping("/tenantManageEnum")
    public ResultEntity tenantManageEnum() {
        return ResultEntity.ok("200","查询类型成功", TenantManage.values());
    }

    /**
     * 获取公私账户
     * @return
     */
    @RequestMapping("/accountTypeEnum")
    public ResultEntity accountTypeEnum() {
        return ResultEntity.ok("200","查询类型成功", AccountTypeEnum.values());
    }

    /**
     * 收房合同录入：下一步 校验合同租约信息等
     */
    @RequestMapping("nextSfContract")
    @ResponseBody
    private ResultEntity nextCfContract(SaveSfContractForm saveSfContractForm) {
        return ResultEntity.ok();
    }

    @RequestMapping("saveSfContract")
    private ResultEntity saveSfContract(@RequestBody SaveSfContractForm saveSfContractForm)
            throws Exception {
        RequestDetailSfContractSaveDto requestDetailSfContractSaveDto = new RequestDetailSfContractSaveDto();
        BeanUtils.copyProperties(saveSfContractForm, requestDetailSfContractSaveDto);
        requestDetailSfContractSaveDto.setDepositMoney(new BigDecimal(saveSfContractForm.getDepositMoney()));
        requestDetailSfContractSaveDto.setMonthRent(new BigDecimal(saveSfContractForm.getMonthRent()));
        requestDetailSfContractSaveDto.setHouseName(saveSfContractForm.getHouseInfo());
        requestDetailSfContractSaveDto.setLesaseStartTime(saveSfContractForm.getStartDate());
        requestDetailSfContractSaveDto.setLeaseEndTime(saveSfContractForm.getEndDate());
        itSfContractService.saveSfContract(requestDetailSfContractSaveDto);
        return ResultEntity.ok();
    }

    /**
     * 根据合同租约信息生成相应账期的付款计划信息
     *
     * @param saveSfContractForm
     * @return
     * @throws ParseException
     */
    @RequestMapping("generatePayMentPlan")
    @ResponseBody
    private Object generatePayMentPlan(@RequestBody SaveSfContractForm saveSfContractForm) throws ParseException {
        Map<String,Object> map = new HashMap<>();
        // 计算生成各账期付款计划
        CalculateFkPlanForm dataPlan = new CalculateFkPlanForm();
        BeanUtils.copyProperties(saveSfContractForm, dataPlan);
        dataPlan.setMonthRent(new BigDecimal(saveSfContractForm.getMonthRent()));// 收房月租金
        dataPlan.setDepositMoney(new BigDecimal(saveSfContractForm.getDepositMoney()));// 房产押金
        List<CwPaymentPlanDto> payMentPlanList = PayMentPlanGenerator.generatePayMentPlan(dataPlan);
        map.put("payMentPlanList",payMentPlanList);
        BigDecimal totalDeposit = new BigDecimal(saveSfContractForm.getDepositMoney())
                .multiply(new BigDecimal(saveSfContractForm.getDepositType()));
        if (BigDecimal.ZERO.equals(totalDeposit)) {
            // 无押金
            map.put("hasDeposit", 0);
        } else {
            // 有押金
            map.put("hasDeposit", 1);
        }
        if (null != saveSfContractForm.getLastContractId()) {
            map.put("isXuYue", 1);// 续约(续约时自动生成的是已付的付款单，支付状态不可编辑)
        } else {
            map.put("isXuYue", 0);
        }
        // 合同总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (CwPaymentPlanDto cwPaymentPlanDto : payMentPlanList) {
            totalAmount = totalAmount.add(cwPaymentPlanDto.getPaymentAmount());
        }

        map.put("totalAmount", totalAmount);
        map.put("totalDeposit",totalDeposit);
        map.put("preCycleChange", 0);
        return map;
    }
}
